package classespinkpony
{
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;

	[RemoteClass(alias='classespinkpony.Practice')]
	public class Practice
	{
		private var _geolocationResult:Boolean;
		private var _id:Number
		private var _user_id:Number;
		private var _profession_id:Number;
		private var _address_id:Number;
		private var _corp_number:String;
		private var _web:String;
		private var _gsm:String;
		private var _phone:String;
		private var _infotext:String;
		
		private var _user_first_name:String;
		private var _user_last_name:String;
		private var _user_avatar:String;
		
		private var _address_street:String;
		private var _address_house_number:String;
		private var _address_box:String;
		private var _address_city_name:String;
		
		private var _location_postal_number:String;
		private var _location_id:Number;
		
		private var _profession_name: String;
		
		private var _country_id:Number;
		
		private var _district_id:Number;
		
		private var _lat:Number;
		private var _lng:Number;
		private var _distance:String;
		//Transient variables
		private var _address: Address;
		
		
		public function Practice(){
			this.address = new Address();
		}


		[Bindable]
		public function set id(id:Number):void{
			this._id = id;
		}
		public function get id():Number{
			return this._id;
		}
		
		[Bindable]
		public function set user_id(user_id:Number):void{
			this._user_id = user_id;
		}
		public function get user_id():Number{
			return this._user_id;
		}
		
		[Bindable]
		public function set profession_id(profession_id:Number):void{
			this._profession_id = profession_id;
		}
		public function get profession_id():Number{
			return this._profession_id;
		}
		
		[Bindable]
		public function set address_id(address_id:Number):void{
			this._address_id = address_id;
		}
		public function get address_id():Number{
			return this._address_id;
		}
		
		[Bindable]
		public function set corp_number(corp_number:String):void{
			this._corp_number = corp_number;
		}
		public function get corp_number():String{
			return this._corp_number;
		}
		
		[Bindable]
		public function set web(web:String):void{
			this._web = web;
		}
		public function get web():String{
			return this._web;
		}
		
		[Bindable]
		public function set gsm(gsm:String):void{
			this._gsm = gsm;
		}
		public function get gsm():String{
			return this._gsm;
		}
		
		[Bindable]
		public function set phone(phone:String):void{
			this._phone = phone;
		}
		public function get phone():String{
			return this._phone;
		}
		
		[Bindable]
		public function set infotext(infotext:String):void{
			this._infotext = infotext;
		}
		public function get infotext():String{
			return this._infotext;
		}

		[Bindable]
		public function get address_city_name():String
		{
			return _address_city_name;
		}
		
		public function set address_city_name(value:String):void
		{
			_address_city_name = value;
		}
		
		
		[Bindable]
		public function get user_first_name():String
		{
			return _user_first_name;
		}

		public function set user_first_name(value:String):void
		{
			_user_first_name = value;
		}

		[Bindable]
		public function get location_postal_number():String
		{
			return _location_postal_number;
		}

		public function set location_postal_number(value:String):void
		{
			_location_postal_number = value;
		}

		[Bindable]
		public function get user_last_name():String
		{
			return _user_last_name;
		}

		public function set user_last_name(value:String):void
		{
			_user_last_name = value;
		}

		[Bindable]
		public function get user_avatar():String
		{
			if(_user_avatar != null && _user_avatar.length > 0){
				return _user_avatar;
			}
			else{
				return "no-avatar.gif";
			}
		}

		public function set user_avatar(value:String):void
		{
			_user_avatar = value;
		}

		[Bindable]
		public function get address_street():String
		{
			return _address_street;
		}

		public function set address_street(value:String):void
		{
			_address_street = value;
		}

		[Bindable]
		public function get address_house_number():String
		{
			return _address_house_number;
		}

		public function set address_house_number(value:String):void
		{
			_address_house_number = value;
		}

		[Bindable]
		public function get address_box():String
		{
			return _address_box;
		}

		public function set address_box(value:String):void
		{
			_address_box = value;
		}

		[Bindable]
		public function get profession_name():String
		{
			return _profession_name;
		}

		public function set profession_name(value:String):void
		{
			_profession_name = value;
		}
		
		[Bindable]
		public function get country_id():Number
		{
			return _country_id;
		}
		
		public function set country_id(value:Number):void
		{
			_country_id = value;
		}

		[Bindable]
		public function get location_id():Number
		{
			return _location_id;
		}
		
		public function set location_id(value:Number):void
		{
			_location_id = value;
		}
		
		//Transient variables (worden niet naar AMFPHP gestuurd)

		[Bindable]
		[Transient]
		public function get address():Address
		{
			return _address;
		}

		public function set address(value:Address):void
		{
			_address = value;
		}
		[Bindable]
		[Transient]
		public function get geolocationResult():Boolean
		{
			return _geolocationResult;
		}
		
		public function set geolocationResult(geolocationResult:Boolean):void
		{
			_geolocationResult = geolocationResult;
		}

		[Bindable]
		public function get district_id():Number
		{
			return _district_id;
		}

		public function set district_id(value:Number):void
		{
			_district_id = value;
		}
		
		[Bindable]
		public function get lat():Number
		{
			return _lat;
		}
		
		public function set lat(lat:Number):void
		{
			_lat = lat;
		}
		[Bindable]
		public function get lng():Number
		{
			return _lng;
		}
		public function set lng(lng:Number):void
		{
			_lng = lng;
		}
		[Bindable]
		public function get distance():String
		{
			return _distance;
		}
		
		public function set distance(distance:String):void
		{
			_distance = distance;
		}
		
		public function calcDistance(visitorLat:Number,visitorLng:Number):void{
			if(visitorLat == 0 && visitorLng ==0)
				distance ="? Km";
			else
				distance = (Utils.distVincenty(lat,lng,visitorLat,visitorLng)/1000).toFixed(2)+" Km";
		}
		
		public function geocodeLatLng():void{
			geolocationResult = false;
			var httpService:HTTPService = new HTTPService();
			var loc:String = address.street + " " + address.house_number + " " + address.box + " " + address.city_name;
			httpService.url = "http://maps.googleapis.com/maps/api/geocode/xml?address="+encodeURI(loc)+"&sensor=false";
			httpService.resultFormat="e4x";
			httpService.method="GET";
			httpService.addEventListener("result", httpResult);
			httpService.addEventListener("fault", httpFault);
			httpService.send();
		}
		private function httpResult(event:ResultEvent):void {
			var xmlStr:String = event.result.toString();
			var xml:XML = event.result as XML;
			if(xml.status=="OK")
			{
				lat = xml.result[0].geometry.location.lat;
				lng = xml.result[0].geometry.location.lng;
				address.lat = lat;
				address.lng = lng;
			}
			geolocationResult = true;
		}
		
		private function httpFault(event:FaultEvent):void {
			var faultstring:String = event.fault.faultString;
			//Alert.show(faultstring);
			geolocationResult = true;
		}
		
	}
}